<?php
// 附件
class AttachModel extends Model {
	protected $tableName = 'x_attach';
		
	// 文件上传
	public function upload() {
		import ( '@.ORG.UploadFile' );
		// 导入上传类
		$upload = new UploadFile ();
		// 设置上传文件大小
		$upload->maxSize = 3292200;
		// 设置上传文件类型
		$upload->allowExts = explode ( ',', 'jpg,gif,png,jpeg' );
		// 设置附件上传目录
		$upload->savePath = UPLOAD_PATH . '/' . date ( 'Ym' ) . '/';
		
		$upload->thumbPath = $upload->savePath;
		
		// 设置需要生成缩略图，仅对图像文件有效
		$upload->thumb = true;
		// 设置引用图片类库包路径
		$upload->imageClassPath = '@.ORG.Image';
		// 设置需要生成缩略图的文件后缀
		$upload->thumbPrefix = 'm_,s_'; // 生产2张缩略图
		                                // 设置缩略图最大宽度
		$upload->thumbMaxWidth = '400,100';
		// 设置缩略图最大高度
		$upload->thumbMaxHeight = '400,100';
		
		if (! $upload->upload ()) {
			// 捕获上传异常
			$info ['status'] = false;
			$info ['error']  = $upload->getErrorMsg ();
			return $info;
		} else {
			// 取得成功上传的文件信息
			$uploadList = $upload->getUploadFileInfo ();
			// import ( '@.ORG.Image' );
			// 给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
			// Image::water ( $uploadList [0] ['savepath'] . 'm_' . $uploadList
		// [0] ['savename'], APP_PATH . 'Tpl/Public/Images/logo.png' );
			// $_POST ['image'] = $uploadList [0] ['savename'];
		}
		
		// 保存当前数据对象
		foreach ( $uploadList as &$data ) {
			$data ['cTime'] = time ();
			$data['savepath'] = str_replace(SITE_PATH, '', $data['savepath']);
			
			$data['attachId'] = $this->add ( $data );
			$data['id'] = $data['attachId'];  //兼容旧的代码
		}
		
		$info ['status'] = true;
		$info ['error']  = '';
		$info['info'] = $uploadList;
		
		return $info;
	}
}
